package memcached

import (
	"time"
)

type Cached struct {
	Key     string
	Flags   uint
	ExpTime *time.Time
	Data    []byte
}

type StoreRequest struct {
	Command string
	Key     string
	Value   *Cached
	Replyc  chan *StoreReply
}

type StoreReply struct {
	Ok    bool
	Value *Cached
}

var cache = make(map[string]*Cached)

func set(value *Cached, out chan<- *StoreReply) {
	cache[value.Key] = value
	out <- &StoreReply{Ok: true}
}

func get(key string, out chan<- *StoreReply) {
	value, ok := cache[key]
	if !ok {
		out <- &StoreReply{Ok: false}
		return
	}
	out <- &StoreReply{
		Ok:    true,
		Value: value,
	}
}

func StoreServer(in <-chan *StoreRequest) {
	for {
		req := <-in
		switch req.Command {
		case "set":
			set(req.Value, req.Replyc)
		case "get":
			get(req.Key, req.Replyc)
		}
	}
}
